package com.raoway.manager 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author gcxk721
	 */
	public class PopUpManager 
	{
		
		public static const CLOSE:String = "close";

        public static var layer:DisplayObjectContainer;
        private static var _dic:Dictionary = new Dictionary(true);
        private static var _modalDic:Dictionary = new Dictionary(true);
		
		public static function createPopUp(cls : Class, interactive:Boolean = true, num : Number = 0) : DisplayObject
		{
            var _display : DisplayObject;
            if (_dic[cls] != undefined)
			{
                _display = _dic[cls];
                setmodal(_display, false);
            } 
			else 
			{
                _display = new cls();
                _dic[cls] = _display;
            }
            layer.addChild(_display);
			
            if (interactive == true)
			{
                if (_display is InteractiveObject)
				{
					
                }
                setmodal(_display, true, 0, _arg3);
            } 
			else 
			{
                setmodal(_display, false);
            }
            centerPopUp(_display);
            _display.addEventListener(Event.CLOSE, closeHandle);
            return _display;
        }
		
        private static function setmodal(_display : DisplayObject, opened : Boolean = true, color : uint = 0, alpha : Number = 0):void
		{
            var mask : Sprite;
            var sprite : Sprite = _modalDic[_display];
            if (opened == false)
			{
                if (sprite != null && layer.containssprite)
				{
                    layer.removeChild(sprite);
                }
                delete _modalDic[_display];
                return;
            }
            if (sprite == null)
			{
                mask = new Sprite();
                mask.graphics.beginFill(color, alpha);
                mask.graphics.drawRect(-(StageProxy.leftOffset()), -(StageProxy.upOffset()), StageProxy.width, StageProxy.height);
                sprite = mask;
                _modalDic[_display] = sprite;
            }
            layer.addChildAt(sprite, layer.getChildIndex(_display));
        }
		
        private static function closeHandle(evt : Event):void
		{
            evt.target.removeEventListener(CLOSE, closeHandle);
            removePopUp(DisplayObject(evt.target));
        }
		
        public static function removePopUp(_display : DisplayObject) : void
		{
            if (_display == null || _display.parent == null)
			{
                return;
            }
            if (_display is DisplayObjectContainer)
			{
                DisplayObjectContainer(_display).mouseChildren = false;
            }
            if (_display is InteractiveObject)
			{
                InteractiveObject(_display).mouseEnabled = false;
            }
            delete _dic[_display["constructor"]];
			
            var sprite : Sprite = _modalDic[_display];
            if (sprite != null && sprite.parent != null)
			{
                sprite.parent.removeChild(sprite);
            }
            delete _modalDic[_display];
            if (_display.parent != null)
			{
                _display.parent.removeChild(_display);
            }
        }
		
        public static function setPopUpPosition(_display : DisplayObject, _x : Number = 0, _y : Number = 0):void
		{
            _arg1.x = _x;
            _arg1.y = _y;
        }
		
        public static function centerPopUp(_display:DisplayObject):void
		{
            _display.x = ((StageProxy.width - _display.width) >> 1) - StageProxy.leftOffset();
            _display.y = ((StageProxy.height - _display.height) >> 1) - StageProxy.upOffset();
        }
		
        public static function hasWindow(cls : Class):Boolean
		{
            return !(_dic[cls] == undefined);
        }
		
        public static function getWindow(cls : Class):DisplayObject
		{
            if (hasWindow(cls) == true)
			{
                return (_dic[cls]);
            }
            return null;
        }
		
	}

}